﻿using DiffisionWithDecomposition;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAnalyzer2
{
    class Program
    {
        static string folderPath = @"C:\Users\cemhe_000\Dropbox\master\Difüzyon\3dDiffision\3dDiffisionWithDecomposition\BinomialSimulatorSimulator\bin\Release\Logs";

        static string folderPath2 = @"C:\Users\cemhe_000\Dropbox\master\Difüzyon\Diffusion With Decomposition\Simulatör Gaye\Logs";

        static void Main(string[] args)
        {
            var files = Directory.GetFiles(folderPath);

            foreach (var file in files)
            {
                try
                {
                    if (file.Contains("_mutualinfo.txt") || file.Contains("_m.txt") || file.Contains("_mutualInformation.txt"))
                    {
                        continue;
                    }

                    AnalyzeCommunicationFile(file);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            /*
            for (int i = 20; i < 500; i += 20)
            {
                if (!File.Exists(folderPath + "\\" + string.Format("Mutual Info D=18, N=1000, Dec=False, HL=2.txt, T_s=100, Duration = 100000.txt", i)))
                {
                    continue;
                }
                try
                {
                    AnalyzeCommunicationFile(folderPath + "\\" + string.Format("Mutual Info D=18, N=1000, Dec=False, HL=2.txt, T_s=100, Duration = 100000.txt", i));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
             * */
        }

        static void AnalyzeCommunicationFile(string fileName)
        {

            var streamWriter = new StreamWriter(fileName.Substring(0, fileName.LastIndexOf(".txt")) + "_m.txt", false);

            var dataList = new int[2][];

            //var extractedInfo = new double[5][];    //threshold total1 total0 correct1 correct0 I

            var allLines = File.ReadAllLines(fileName);

            dataList[0] = new int[allLines.Count()];
            dataList[1] = new int[allLines.Count()];

            for (int i = 0; i < allLines.Length; i++)
            {
                var t = allLines[i].Split(' ');
                dataList[0][i] = int.Parse(t[0]);
                dataList[1][i] = int.Parse(t[1]);
            }

            //linear search mutual informations

            var threshold = 0;
            var thresholdStepSize = 1;

            for (int i = 0; i < 1000; i++)   //emprically try out 6000 threshold values
            {
                var total0 = 0;
                var total1 = 0;
                var correct0 = 0;
                var correct1 = 0;

                for (int k = 0; k < dataList[0].Length; k++)  // 100 bit warm up period
                {
                    if (dataList[0][k] == 0)
                    {
                        total0++;

                        if (dataList[1][k] <= threshold)
                        {
                            correct0++;
                        }
                    }
                    else
                    {
                        total1++;

                        if (dataList[1][k] > threshold)
                        {
                            correct1++;
                        }
                    }
                }

                //streamWriter.WriteLine(correct1 / (double)total1);


                var I = ShannonsMutualInformation.MutualInformation(total0, total1, correct1, correct0);

                streamWriter.WriteLine(threshold + " " + correct1 / (double) total1 + " " + correct0 / (double) total0 + " " + I);
                //streamWriter.WriteLine(threshold + " " + total1 + " " + total0 + " " + correct1 + " " + correct0 + " " + I);

                threshold += thresholdStepSize;
            }

            streamWriter.Flush();
            streamWriter.Close();
        }
    }
}
